package com.gongJiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gongJiao.Vo.PageCondition;
import com.gongJiao.entity.Site;
import com.gongJiao.entity.User;
import com.gongJiao.mapper.UserMapper;
import com.gongJiao.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    /**
     * 根据用户名查找用户名是否存在
     * @param username
     * @return
     */
    @Override
    public Boolean getUserNameByUserName(String username) {
        String result=userMapper.getUserNameByUserName(username);
        if (result == null) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据用户名和密码查询用户
     * @param username
     * @param password
     * @return
     */
    @Override
    public User getUser(String username, String password) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username).eq("password", password));
        return user;
    }

    /**
     * 后台分页查询用户信息列表
     * @return
     */
    @Override
    public List<User> getUserList(PageCondition pageCondition) {
        return userMapper.getUserList(pageCondition);
    }

    /**
     * 后台分页查询用户信息数量
     * @return
     */
    @Override
    public Integer getCount(PageCondition pageCondition) {
        return userMapper.getCount(pageCondition);
    }

    /**
     * 删除用户信息
     * @param ids
     * @return
     */
    @Override
    public void deleteUserByIds(String ids) {
        String[] idArray = ids.split(",");
        List idList=new ArrayList<>();
        for (String id : idArray) {
            idList.add(id);
        }
        userMapper.deleteBatchIds(idList);
    }

    /**
     * 根据用户名查询用户
     * @param name
     * @return
     */
    @Override
    public User getUserByName(String name) {
        return userMapper.getUserByName(name);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Override
    public Boolean updateUser(User user) {
        int i = userMapper.updateById(user);
        if (i > 0) {
            return true;
        }
        return false;
    }
}
